package org.rowan.adi.components.comp2d;

import java.util.LinkedList;
import org.rowan.adi.Vector;

/**
 * The Point2D class represents a point in two dimensional space.
 *
 * @author Robert Russell
 * @version 1.0
 */
public class Point2D extends Component2D {

    /**
     * Constructs an instance of Point2D
     * @param location the location of the 2D point.
     */
    public Point2D(Vector location) {
        LinkedList<Point2D> points = new LinkedList<Point2D>();
        points.add(this);
	Vector roundedLoc = new Vector(round(location.getX()), round(location.getY()));
        super.setListofPoints(points);
        super.setLocation(roundedLoc);
        super.setTrajectory(Vector.ZERO_VECTOR);
    }

    /**
     * Gets the X location of the 2D point.
     * @return the X location of the 2D point.
     */
    public double getX() {
        return super.getLocation().getX();
    }

    /**
     * Gets the Y location of the 2D point.
     * @return the Y location of the 2D point.
     */
    public double getY() {
        return super.getLocation().getY();
    }

    /**
     * Sets the X location of the 2D point to a given value.
     * @param x the new X location.
     */
    public void setX(double x) {
        super.setLocation(new Vector(round(x), getLocation().getY()));
    }

    /**
     * Sets the Y location of the 2D point to a given value.
     * @param y the new Y location.
     */
    public void setY(double y) {
        super.setLocation(new Vector(getLocation().getX(), round(y)));
    }

    /**
     * Rounds the x and y coordinates to a given  number of decimal places after
     * the period.
     * @param number to be rounded.
     * @return the new number rounded to certain decimal places.
     */
    private double round(double number) {
        int decimalPlaces = 4;
	return (double) ((int) (number * Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces);
    }

    /**
     * Provides a string of the x and y coordinates in parentheses. (x,y)
     * @return the String of the points x and y
     */
    public String toString() {
        return "(" + super.getLocation().getX() + "," + super.getLocation().getY() + ")";
    }
}
